const Voted = {
	isSupport() {
		if (typeof(window) === "undefined") return false;
		let passed = false;
		try {
			passed = 'localStorage' in window && window.localStorage != null;
		} catch (e) {
			/* If we get a security exception we know the feature exists, but cookies are disabled */
		}
		return passed;
	},
	get() {
		if (!Voted.isSupport()) {
			return [];
		}
		const voted = window.localStorage.getItem('voted');
		if (voted) {
			return voted.split(',');
		} else {
			return [];
		}
	},
	is(id: string) {
		if (!Voted.isSupport()) {
			return true;
		}
		return Voted.get().includes(id);
	},
	add(id: string) {
		if (!Voted.isSupport()) {
			return;
		}
		const res = Voted.get();
		if (!res.includes(id)) {
			res.push(id);
			window.localStorage.setItem('voted', res.join(','));
		}
	}
}

export default Voted;